package plus.easydo.easypush.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.zhyd.oauth.cache.AuthStateCache;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import plus.easydo.core.result.DataResult;
import plus.easydo.core.result.R;
import plus.easydo.easypush.annotation.AuthCheck;
import plus.easydo.easypush.dto.ChannelInfoDto;
import plus.easydo.easypush.entity.ChannelInfo;
import plus.easydo.easypush.oauth.AuthStateRedisCache;
import plus.easydo.easypush.service.ChannelInfoService;
import plus.easydo.easypush.vo.ChannelInfoVo;
import plus.easydo.easypush.vo.UserInfoVo;
import plus.easydo.starter.mybatis.plus.base.MPBaseController;
import plus.easydo.starter.mybatis.plus.qo.MpBaseQo;
import plus.easydo.starter.mybatis.plus.result.MPDataResult;
import plus.easydo.utils.ResultConvertUtil;



/**
 * @author laoyu
 * @version 1.0
 * @date 2022/2/26
 */
@RestController
@Api(tags = "渠道相关")
@RequestMapping("/channel")
public class ChannelController extends MPBaseController {

    @Autowired
    ChannelInfoService channelInfoService;

    @Autowired
    AuthStateRedisCache authStateRedisCache;


    @AuthCheck
    @ApiOperation("用户渠道信息分页查询")
    @PostMapping("/page")
    public MPDataResult userChanelPage(@RequestHeader("token")String token, @RequestBody MpBaseQo baseQo) {
        LambdaQueryWrapper<ChannelInfo> wrapper = Wrappers.lambdaQuery();
        UserInfoVo user = authStateRedisCache.getUserCacheByToken(token);
        if(!user.isAdmin()){
            wrapper.eq(ChannelInfo::getUserId,user.getId());
        }
        IPage<ChannelInfo> resource = channelInfoService.page(baseQo.startPage(),wrapper);
        Page<ChannelInfoVo> voPage = new Page<>();
        BeanUtils.copyProperties(resource,voPage);
        voPage.setRecords(ResultConvertUtil.copyList(resource.getRecords(), ChannelInfoVo::new));
       return ok(voPage);
    }


    @ApiOperation("渠道信息详情")
    @GetMapping("/info/{id}")
    public R<ChannelInfoVo> info(@PathVariable("id") Long id) {
        return DataResult.ok(ResultConvertUtil.convertVo(channelInfoService.getById(id),ChannelInfoVo::new));
    }


    @ApiOperation("添加渠道信息")
    @PostMapping("/add")
    public R<Object> add(@RequestBody ChannelInfoDto channelInfoDto) {
        ChannelInfo entity = ResultConvertUtil.convertVo(channelInfoDto,ChannelInfo::new);
        entity.buildTimeAll();
        return opResult(channelInfoService.save(entity));
    }


    @ApiOperation("更新渠道信息")
    @PostMapping("/update")
    public R<Object> update(@RequestBody ChannelInfoDto channelInfoDto) {
        ChannelInfo entity = ResultConvertUtil.convertVo(channelInfoDto,ChannelInfo::new);
        entity.buildUpdateTime();
        return opResult(channelInfoService.updateById(entity));
    }

    @ApiOperation("删除渠道信息")
    @GetMapping("/remove/{id}")
    public R<Object> remove(@PathVariable("id") Long id) {
        return opResult(channelInfoService.removeById(id));
    }


}
